#include "editorpanel.h"
#include "windows.h"
#include "mixedbutton.h"
#include "tabbedpanel.h"

int EditorPanel::indexOfLetter(const char * a_str, const char & a_letter, const int & a_start)
{int i;for(i=a_start;a_str[i]&&a_str[i]!=a_letter;++i);return i;}

int EditorPanel::indexOfLetterLast(const char * a_str, const char & a_letter, const int & a_end)
{int i;for(i=a_end-1;i>0&&a_str[i]!=a_letter;--i);return i;}

int EditorPanel::parseInt(const char * a_str, const int & len, int & a_index)
{
	int value = 0;
	bool isNumber = true;
	bool isNegative = (a_str[a_index] == '-');
	if(isNegative)a_index++;
	char c;
	for(; isNumber && a_index < len; ++a_index)
	{
		c = a_str[a_index];
		isNumber = (c >= '0' && c <= '9');
		if(isNumber)
		{
			value *= 10;
			value += c-'0';
		}
	}
	if(isNegative)
	{
		value *= -1;
	}
	return value;
}

const char* EditorPanel::getFilenameFromPath(const char * a_path, char * a_out_name, const int & a_outSize)
{
	if(!a_path)
	{
		a_out_name[0] = '\0';
		return a_out_name;
	}
	int start = indexOfLetterLast(a_path, '/', strlen(a_path));
	if(start == 0)
		start = indexOfLetterLast(a_path, '\\', strlen(a_path));
	if(a_path[start] == '/' || a_path[start] == '\\')++start;
	// get the end of the name
	int end = indexOfLetter(a_path, '.', start);
	int nameLen = end-start;
	if(nameLen >= a_outSize)nameLen = a_outSize-1;
	for(int i = 0; i < nameLen; ++i)
	{
		a_out_name[i] = a_path[i+start];
	}
	a_out_name[nameLen] = '\0';
	return a_out_name;
}

static WCHAR * charToWCHAR(const char * a_str)
{
	if(!a_str)return 0;
	int size = strlen(a_str);
	WCHAR * result = new WCHAR[size+1];
	for(int i = 0; i < size; ++i)
		result[i] = a_str[i];
	result[size] = '\0';
	return result;
}

void EditorPanel::fileDialog(const char * a_title, char* a_startPath, 
				char* a_filter, char * a_out_filePath, 
				const int & a_filePathMaxSize, const char * a_default)
{
    WCHAR filestring[256];
	if(a_default){
		int i = 0;for(; a_default[i]; ++i){filestring[i] = a_default[i];}filestring[i] = '\0';
	}else{
		filestring[0] = 0;
	}
	WCHAR * title = charToWCHAR(a_title);
	WCHAR * path = charToWCHAR(a_startPath);
	WCHAR * filter = charToWCHAR(a_filter);
    OPENFILENAME opf;
    opf.hwndOwner = 0;
    opf.lpstrFilter = filter;
    opf.lpstrCustomFilter = 0;
    opf.nMaxCustFilter = 0L;
    opf.nFilterIndex = 1L;
    opf.lpstrFile = filestring;
    opf.nMaxFile = 256;
    opf.lpstrFileTitle = 0;
    opf.nMaxFileTitle=50;
    opf.lpstrInitialDir = path;
    opf.lpstrTitle = title;
    opf.nFileOffset = 0;
    opf.nFileExtension = 0;
    opf.lpstrDefExt = L"*.*";
    opf.lpfnHook = NULL;
    opf.lCustData = 0;
    opf.Flags = (OFN_HIDEREADONLY | OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT) & ~OFN_ALLOWMULTISELECT;
    opf.lStructSize = sizeof(OPENFILENAME);
	int i = 0;
    if(GetOpenFileName(&opf))
    {
		// copy the WCHAR* data into a regular char*
		for(i = 0; i < a_filePathMaxSize && opf.lpstrFile[i]; ++i)
			a_out_filePath[i] = (char)opf.lpstrFile[i];
	}
	// null-terminate the string
	a_out_filePath[i] = '\0';
	if(filter)	delete filter;
	if(path)	delete path;
}

const int BUFFER_SIZE = 1024;

bool EditorPanel::load(const char * a_path)
{
	bool success = loadMethod(a_path);
	if(success)
	{
		setFileName(a_path);
		updateTabTextFromPath(a_path);
		if(m_notifier)
			m_notifier(m_whoToNotify, this);
	}
	return success;
}
bool EditorPanel::save(const char * a_path)
{
	bool success = saveMethod(a_path);
	if(success)
	{
		setFileName(a_path);
		updateTabTextFromPath(a_path);
	}
	return success;
}

bool EditorPanel::save()
{
	char buffer[BUFFER_SIZE];
	fileDialog("Save", 
		m_filename, 0, buffer, BUFFER_SIZE, getFileName());
	return (buffer[0] && save(buffer));
}
bool EditorPanel::load()
{
	char buffer[BUFFER_SIZE];
	fileDialog("Load", ".", 0, buffer, BUFFER_SIZE, m_filename);
	setFileName(buffer);
	bool loaded = (buffer[0] && load(buffer));
	if(!loaded){setFileName(0);}
	return loaded;
}

void EditorPanel::updateTabTextFromPath(const char * a_path)
{
	const int BUFFER_SIZE = 1024;
	int nameSize = BUFFER_SIZE;
	char name[BUFFER_SIZE];
	getFilenameFromPath(a_path, name, nameSize);
	updateTabText(name);
}

void EditorPanel::updateTabText(const char * a_text)
{
	// modify the tab button
	TabbedPanel * tp = (TabbedPanel*)this->getParent();
	MixedButton * b = (MixedButton*)tp->getButtonOf(this);
	if(b)
	{
		b->setDirty(0);
		ImageClipStrip * ics = b->getTextGraphics();
		ics->createStringStrip(a_text);
		tp->refreshTabs();
		b->setDirty(0);
	}
}
